<template>
  <div class="dialog-form">
    <el-form ref="refundRef" :model="form" :rules="rules" label-width="100">
      <el-form-item label="来源渠道：">{{ form.channelCode }}</el-form-item>
      <el-form-item label="用户编号：">{{ form.platformUserId }}</el-form-item>
      <el-form-item label="电子卡号：">{{ form.cardNo }}</el-form-item>
      <el-form-item label="手机号码：">{{ form.mobile }}</el-form-item>
      <el-form-item label="退款金额：">{{ formatAmount(form.refundFee) }}元</el-form-item>
      <el-form-item label="申请原因：">{{ form.applicationDesc }}</el-form-item>
      <el-form-item v-if="form.state == 1" label="申请时间：">{{ form.createTime }}</el-form-item>
      <el-form-item v-else label="审核时间：">
        {{ 
           form.state == 2 ? form.throughTime : form.rejectTime
        }}
      </el-form-item>
      <el-form-item v-if="form.state == 2" label="审核状态：">
        <dict-tag :options="refund_status" :value="form.state" />
      </el-form-item>
      <el-form-item v-if="form.state == 1" label="审核备注：" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="4" placeholder="请输入审核备注，最多200个字" :maxlength="200" />
      </el-form-item>
      <el-form-item v-else label="审核备注：">{{ form.approvalDesc }}</el-form-item>
    </el-form>
    <div class="dialog-footer">
      <template v-if="form.state == 1">
        <el-button @click="onConfirm(3)">驳回</el-button>
        <el-button type="primary" @click="onConfirm(2)">通过</el-button>
      </template>
      <el-button v-else type="primary" @click="onCancel">关闭</el-button>
    </div>
  </div>
</template>

<script setup>
import { getRefund, updateRefund } from "@/api/order/refund"

const { proxy } = getCurrentInstance()
const { refund_status } = proxy.useDict('refund_status')

const refundRef = ref()
const emits = defineEmits(['cancel', 'confirm'])

const type = ref(0)            // 1审核  2详情

const form = ref({
  id: null,
  remark: null,
})
const rules = {
  remark: [
    { required: true, message: "请输入审核备注", trigger: "blur" }
  ],
}

const pageInit = (id, val) => {
  type.value = val
  getRefund(id).then(res => {
    form.value = res.data || {}
  })
}


const onCancel = () => {
  emits('cancel')
}

const onConfirm = (state) => {
  refundRef.value.validate(valid => {
    if (valid) {
      const params = {
        id: form.value.id,
        state: state,
        remark: form.value.remark
      }
      updateRefund(params).then(res => {
        proxy.$modal.msgSuccess("审核成功")
        emits('confirm')
      })
    }
  })
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>